import { _decorator, Component, Label, Node, ResolutionPolicy, size, UITransform, v3, Vec3, view } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LHJCSWP_FitScreen')
export class LHJCSWP_FitScreen extends Component {
    
    @property
    curPlatform = "applovin";

    @property(Node)
    gameContent : Node = null;

    @property(Node)
    gameFix : Node[] = [];
    
    isLandscape = false;

    isPad = false;

    titleNode = null;

    downNode = null;

    start() {

        this.titleNode = this.gameContent.getChildByName("title");
        this.downNode = this.gameContent.getChildByName("download");

        this.checkUpdate();
        this.updatePosition();
        this.setTitle();
        view.setResizeCallback(() => {
            console.log("出现屏幕切换")
            this.checkUpdate();
            this.updatePosition();
            this.setTitle();
        })
    }

    setTitle(){
        
        let platform = this.curPlatform;
        if(platform == "mintegral" || platform == "moloco" || platform == "unity"){
            this.titleNode.active = true;
            this.downNode.getChildByName("title").active = false;
            this.titleNode.getComponent(Label).string = "The final result is not guaranteed, and the amount you can get is subject to the rules published in the APP.";
        }else if(platform == "ironsource"){
            this.titleNode.active = true;
            this.downNode.getChildByName("title").active = false;
            this.titleNode.getComponent(Label).string = "For illustration purposes only. No rewards are provided for ad play or completion.";
        }else if(platform == "applovin"){
            this.titleNode.active = false;
            this.downNode.getChildByName("title").active = true;
        }

        let width = this.gameContent.getComponent(UITransform).width;
        this.titleNode.getComponent(UITransform).width = width - 80;
    }

    checkUpdate(){
        let width = view.getViewportRect().width;
        let height = view.getViewportRect().height;
        this.isPad = false;

        if (width > height) {
            this.gameFix[0].active = false;
            this.gameFix[1].active = true;
            this.isLandscape = true;
            this.gameContent.getComponent(UITransform).setContentSize(size(1334, 750));
            view.setDesignResolutionSize(1334, 750, ResolutionPolicy.FIXED_HEIGHT);
            if (width / height <= 1.5) {
                console.log("ipad");
                this.isPad = true;
                view.setDesignResolutionSize(750, 1334, 5);
            }
        } else {
            this.gameFix[0].active = true;
            this.gameFix[1].active = false;
            this.gameContent.getComponent(UITransform).setContentSize(size(750, 1334));
            this.isLandscape = false;
            view.setDesignResolutionSize(750, 1334, ResolutionPolicy.FIXED_WIDTH);
            if (width / height > 0.65) {
                console.log("ipad");
                this.isPad = true;
                view.setDesignResolutionSize(750, 1334, 3);
            }
        }
    }

    updatePosition(){
        let width = this.gameContent.getComponent(UITransform).width;
        let height = this.gameContent.getComponent(UITransform).height;

        let download_x = width / 2 - this.downNode.getComponent(UITransform).width / 2;
        let download_y = -height / 2 + this.downNode.getComponent(UITransform).height / 2;
        this.downNode.setPosition(new Vec3(download_x,download_y,0));

        if(this.isLandscape){

            console.log("横屏适配");
            let gamePlane = this.gameContent.getChildByName("gamePlane");
            let game_height = gamePlane.getComponent(UITransform).height;
            let game_width = gamePlane.getComponent(UITransform).width;
            let game_scale = height / (game_height + 300);
            gamePlane.setScale(game_scale,game_scale,game_scale);

            let machine = this.gameContent.getChildByName("machine");
            let mac_height = machine.getComponent(UITransform).height;
            let mac_width = machine.getComponent(UITransform).width;
            let mac_scale = height / (mac_height + 300);
            machine.setScale(mac_scale,mac_scale,mac_scale);

            let topPlane = this.gameContent.getChildByName("topPlane");
            let top_height = topPlane.getComponent(UITransform).height;
            let top_width = topPlane.getComponent(UITransform).width;
            let top_scale = height / (top_height + 500);
            topPlane.setScale(top_scale,top_scale,top_scale);

            let bottomPlane = this.gameContent.getChildByName("bottomPlane");
            let bot_height = bottomPlane.getComponent(UITransform).height;
            let bot_width = bottomPlane.getComponent(UITransform).width;
            let bot_scale = height / (bot_height + 800);
            bottomPlane.setScale(bot_scale,bot_scale,bot_scale);

            gamePlane.setPosition(v3(-width / 4,0,0));
            machine.setPosition(v3(0,0,0));
            bottomPlane.setPosition(v3(width / 4,-height / 4,0));
            topPlane.setPosition(v3(width / 4,height / 8,0));

            let endPlane = this.gameContent.getChildByName("endPlane");
            let end_scale = height / (endPlane.getComponent(UITransform).height + 100);
            endPlane.setScale(end_scale,end_scale,end_scale);
            endPlane.setPosition(v3(0,0,0));

            if(this.isPad){
                console.log("IPad适配");
                let gamePlane = this.gameContent.getChildByName("gamePlane");
                let game_height = gamePlane.getComponent(UITransform).height;
                let game_width = gamePlane.getComponent(UITransform).width;
                let game_scale = height / (game_height + 10);
                gamePlane.setScale(game_scale,game_scale,game_scale);

                let machine = this.gameContent.getChildByName("machine");
                let mac_height = machine.getComponent(UITransform).height;
                let mac_width = machine.getComponent(UITransform).width;
                let mac_scale = height / (mac_height - 100);
                machine.setScale(mac_scale,mac_scale,mac_scale);

                let bottomPlane = this.gameContent.getChildByName("bottomPlane");
                let bot_height = bottomPlane.getComponent(UITransform).height;
                let bot_width = bottomPlane.getComponent(UITransform).width;
                let bot_scale = height / (bot_height + 600);
                bottomPlane.setScale(bot_scale,bot_scale,bot_scale);

                let topPlane = this.gameContent.getChildByName("topPlane");
                let top_height = topPlane.getComponent(UITransform).height;
                let top_width = topPlane.getComponent(UITransform).width;
                let top_scale = height / (top_height + 400);
                topPlane.setScale(top_scale,top_scale,top_scale);

                gamePlane.setPosition(v3(0,-height / 2 + game_height  / 2 * game_scale,0));
                machine.setPosition(v3(0,0,0));
                bottomPlane.setPosition(v3(0,-height / 2 + bot_height / 2 * bot_scale - 200,0));
                topPlane.setPosition(v3(0,height / 2 - top_height  / 2 * top_scale + 270,0));

                let endPlane = this.gameContent.getChildByName("endPlane");
                let end_scale = height / (endPlane.getComponent(UITransform).height - 100);
                endPlane.setScale(end_scale,end_scale,end_scale);
                endPlane.setPosition(v3(0,0,0));
            }

        }else{
            console.log("竖屏适配");
            let gamePlane = this.gameContent.getChildByName("gamePlane");
            let game_height = gamePlane.getComponent(UITransform).height;
            let game_width = gamePlane.getComponent(UITransform).width;
            let game_scale = width / (game_width + 30);
            gamePlane.setScale(game_scale,game_scale,game_scale);

            let machine = this.gameContent.getChildByName("machine");
            let mac_height = machine.getComponent(UITransform).height;
            let mac_width = machine.getComponent(UITransform).width;
            let mac_scale = width / (mac_width + 30);
            machine.setScale(mac_scale,mac_scale,mac_scale);

            let bottomPlane = this.gameContent.getChildByName("bottomPlane");
            let bot_height = bottomPlane.getComponent(UITransform).height;
            let bot_width = bottomPlane.getComponent(UITransform).width;
            let bot_scale = width / (mac_width + 50);
            bottomPlane.setScale(bot_scale,bot_scale,bot_scale);

            let topPlane = this.gameContent.getChildByName("topPlane");
            let top_height = topPlane.getComponent(UITransform).height;
            let top_width = topPlane.getComponent(UITransform).width;
            let top_scale = width / (top_width + 50);
            topPlane.setScale(top_scale,top_scale,top_scale);

            gamePlane.setPosition(v3(0,0,0));
            machine.setPosition(v3(0,0,0));
            bottomPlane.setPosition(v3(0,-height / 2 + bot_height / 2 * bot_scale + 100,0));
            topPlane.setPosition(v3(0,height / 2 - top_height  / 2 * top_scale - 50,0));

            let endPlane = this.gameContent.getChildByName("endPlane");
            let end_scale = width / (endPlane.getComponent(UITransform).width + 150);
            endPlane.setScale(end_scale,end_scale,end_scale);
            endPlane.setPosition(v3(0,0,0));

            if(this.isPad){

                console.log("IPad适配");

                let gamePlane = this.gameContent.getChildByName("gamePlane");
                let game_height = gamePlane.getComponent(UITransform).height;
                let game_width = gamePlane.getComponent(UITransform).width;
                let game_scale = width / (game_width);
                gamePlane.setScale(game_scale,game_scale,game_scale);

                let machine = this.gameContent.getChildByName("machine");
                let mac_height = machine.getComponent(UITransform).height;
                let mac_width = machine.getComponent(UITransform).width;
                let mac_scale = width / (mac_width + 30);
                machine.setScale(mac_scale,mac_scale,mac_scale);

                let bottomPlane = this.gameContent.getChildByName("bottomPlane");
                let bot_height = bottomPlane.getComponent(UITransform).height;
                let bot_width = bottomPlane.getComponent(UITransform).width;
                let bot_scale = width / (mac_width + 50);
                bottomPlane.setScale(bot_scale,bot_scale,bot_scale);

                let topPlane = this.gameContent.getChildByName("topPlane");
                let top_height = topPlane.getComponent(UITransform).height;
                let top_width = topPlane.getComponent(UITransform).width;
                let top_scale = width / (top_width + 50);
                topPlane.setScale(top_scale,top_scale,top_scale);

                gamePlane.setPosition(v3(0,0,0));
                machine.setPosition(v3(0,0,0));
                bottomPlane.setPosition(v3(0,-height / 2 + bot_height / 2 * bot_scale + 50,0));
                topPlane.setPosition(v3(0,height / 2 - top_height  / 2 * top_scale - 50,0));

                let endPlane = this.gameContent.getChildByName("endPlane");
                let end_scale = width / (endPlane.getComponent(UITransform).width + 50);
                endPlane.setScale(end_scale,end_scale,end_scale);
                endPlane.setPosition(v3(0,0,0));
            }
        }
    }  
}

